﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using BCCommon;
using BCCommon.Enums.Chats;
using BCEntity.Chats;

namespace BCData.Chats
{
    public class ThreadParticipantData : IThreadParticipantData
    {
        private readonly IDatabaseContext databaseContext;

        public ThreadParticipantData(IDatabaseContext databaseContext)
        {
            this.databaseContext=databaseContext;
        }

        public async Task<IEnumerable<ThreadParticipantEntity>> GetThreadParticipantsByThreadIdAsync(IEnumerable<long> threadIds)
        {
            if (!threadIds.Any())
            {
                return new ThreadParticipantEntity[0];
            }

            string sql = typeof(ThreadParticipantEntity).GetSelectSql();

            return await this.databaseContext.Slave.QueryAsync<ThreadParticipantEntity>($"{sql} WHERE Status = '{ThreadParticipantStatus.Joined}' AND ThreadId IN ?ThreadIds", new { threadIds });
        }

        public Task<int> InsertAllAsync(IEnumerable<ThreadParticipantEntity> entities)
        {
            return this.databaseContext.Master.InsertAllAsync(entities, this.databaseContext.Transaction);
        }

        public Task<int> UpdateAllAsync(IEnumerable<ThreadParticipantEntity> entities)
        {
            return this.databaseContext.Master.UpdateAllAsync(entities, this.databaseContext.Transaction);
        }
    }
}
